package com.ziyun.pop.web.modules.mercharReport.controller;

import com.ziyun.pop.common.utils.FileUtil;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.mercharReport.entity.ReportOrderInfoVO;
import com.ziyun.pop.modules.mercharReport.entity.TwbReportOrderInfoVO;
import com.ziyun.pop.modules.mercharReport.service.MercharOrderInfoService;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 值班账单表
 *
 * @author jiangli
 * @email
 * @date 2018-10-16 14:34:22
 */
@RestController
@RequestMapping("mercharReport/orderinfo")
public class ReportOrderInfoController {
    @Autowired
    private MercharOrderInfoService orderInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mercharReport:orderinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        params.put("sellerId", curentUser.getSellerId());
        return orderInfoService.list(params);
    }
    @RequestMapping("/twblist")
    public R twblist(@RequestBody Map<String, Object> params) {
        SysUserEntity curentUser = UserUtils.getCurentUser();
        params.put("sellerId", curentUser.getSellerId());
        return orderInfoService.list(params);
    }

    /**
     * excel 导出
     */
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        String data = request.getParameter("postData");
        Map<String, Object> params = JsonUtils.string2Map(data);
        params.put("sellerId", UserUtils.getCurentUser().getSellerId());
        // 查询列表数据
        List<ReportOrderInfoVO> list = orderInfoService.exportExcel(params);
        FileUtil.exportExcel(list, "值班账单表", "值班账单表", ReportOrderInfoVO.class, "值班账单表.xls", response);
    }

    @RequestMapping("/twvExportExcel")
    public void twvExportExcel(HttpServletRequest request, HttpServletResponse response) {
        String data = request.getParameter("postData");
        Map<String, Object> params = JsonUtils.string2Map(data);
        params.put("sellerId", UserUtils.getCurentUser().getSellerId());
        // 查询列表数据
        List<ReportOrderInfoVO> list = orderInfoService.exportExcel(params);
        List<TwbReportOrderInfoVO> twbList=new ArrayList<>();
        for (ReportOrderInfoVO reportOrderInfoVO : list) {
            TwbReportOrderInfoVO twb = new TwbReportOrderInfoVO();
            BeanUtils.copyProperties(reportOrderInfoVO,twb);
            twbList.add(twb);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String date = format.format(new Date());
        FileUtil.exportExcel(twbList, "值班账单表", "值班账单表", TwbReportOrderInfoVO.class, "值班账单表"+date+".xls", response);
    }

}
